package myThread;

import java.util.Timer;
import java.util.TimerTask;

public class demo34 {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello 3000");
            }
        },3000);

        TimerTask task2 = new TimerTask() {
            private int count = 0;
            @Override
            public void run() {
                count++;
                System.out.println("hello task");
                if(count>=5){
                    this.cancel();//取消当前任务
                    timer.cancel();//关闭定时器
                    System.out.println("定时器已关闭");
                }

            }
        };
        timer.schedule(task2,1000,1000);
    }
}
